package com.shareach.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.shareach.common.BaseQueryOrder;
import com.shareach.dao.redis.impl.DeployRDao;
import com.shareach.dao.redis.impl.DeployRDao.DeployType;
import com.shareach.domain.BaseCityDomain;
import com.shareach.domain.BizTicketDomain;
import com.shareach.service.BaseCityService;
import com.shareach.service.BaseShopService;
import com.shareach.service.BizTicketService;
import com.shareach.util.JsonUtil;
import com.shareach.web.basic.Answer;

@Controller("wticket")
@RequestMapping("/wticket")
public class TicketController extends BaseController {
	@Autowired
	private BaseShopService shopService;
	@Autowired
	private BizTicketService ticketService;
	@Autowired
	private BaseCityService cityService;
	@Autowired
	private DeployRDao deployRDao;

	@RequestMapping(value = "/index.htm")
	public ModelAndView summary(HttpServletRequest request, HttpServletResponse response, String name, Long cityId,
			Long id, String title, Integer hasdeploy, Integer hasmatch) {
		ModelAndView mv = new ModelAndView("admin/discount/ticket/index");
		BaseQueryOrder order = new BaseQueryOrder();
		order.setNumber(getPageNo(request).toString());
		order.setPageSize(getPageSize(request));
		order.addSort("_id", Order.DESCENDING.toString());
		BizTicketDomain ticketDomain = new BizTicketDomain();
		addCriteria(ticketDomain, hasdeploy, "deployTime");
		addCriteria(ticketDomain, hasmatch, "shopId");

		if (StringUtils.isNotEmpty(name)) {
			String _name = name.trim();
			ticketDomain.addRegexMap(_name, new String[] { "name", "brandName", "orgName" });
			mv.addObject("name", _name);
		}
		if (StringUtils.isNotEmpty(title)) {
			ticketDomain.addRegexMap(title, "title");
			mv.addObject("title", title);
		}
		if (null != id) {
			ticketDomain.setId(id);
		}
		if (cityId != null) {
			ticketDomain.setCity(cityId);
			mv.addObject("cityId", cityId);
		}

		Page<BizTicketDomain> result = ticketService.getPageResult(ticketDomain, order);
		List<BizTicketDomain> ticketList = result.getContent();
		for (BizTicketDomain bizTicket : ticketList) {
			if (bizTicket.getShopId() != null && bizTicket.getShopId() > 0) {
				bizTicket.setBaseShop(shopService.getById(bizTicket.getShopId()));
			}
		}

		List<BaseCityDomain> cities = cityService.findAll();
		mv.addObject("cities", cities);
		mv.addObject("result", result);
		mv.addObject("id", id);
		mv.addObject("deploy", hasdeploy);
		mv.addObject("match", hasmatch);
		return mv;
	}

	@RequestMapping(value = "/edit.htm")
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response, String ticketId,
			BizTicketDomain domain) {
		ModelAndView view = new ModelAndView("admin/discount/ticket/edit");
		if (domain == null || domain.getId() == null) {
			// redirect
			Long parabrand = Long.valueOf(ticketId);
			BizTicketDomain ticket = ticketService.getById(parabrand);
			view.addObject("ticket", ticket);
			return view;
		} else {
			Answer<Object> r = new Answer<Object>();
			try {
				BizTicketDomain ticket = ticketService.getById(domain.getId());
				BizTicketDomain change = ticket.compareTo(domain);
				change.setId(domain.getId());
				// edit
				domain.setName(null);// 此属性不可更新
				ticketService.update(change);
				// update redis
				resetRedisDeployed(DeployType.Ticket, domain.getId());
				this.outText(response, JsonUtil.jsonFromObject(r));
			} catch (Exception e) {
				r.setCmd(-1);
				r.setMsg("System Error");
				this.outText(response, JsonUtil.jsonFromObject(r));
				e.printStackTrace();
			}
			return view;
		}
	}

	private void addCriteria(BizTicketDomain domain, Integer val, String propName) {
		if (null == val) {
			return;
		}
		switch (val) {
		case 2:
			domain.addFieldExists(propName);
			break;
		case 3:
			domain.addFieldNoneExists(propName);
			break;
		}
	}

	public void resetRedisDeployed(DeployType type, Long id) {
		if (deployRDao.isDeployed(type, id)) {
			deployRDao.setUndeployed(type, id);
		}
	}
}
